package com.caigou.comm.base.ext

import java.util.*

/**
 * @author #Suyghur.
 * Created on 2022/01/04
 */

/**
 * 计算指定月份的天数
 *
 * @param year
 * @param month
 * @return
 */
fun getMonthDays(year: Int, month: Int): Int {
    return when (month) {
        1, 3, 5, 7, 8, 10, 12 -> 31
        4, 6, 9, 11 -> 30
        2 -> if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
            29
        } else {
            28
        }
        else -> -1
    }
}

/**
 * 计算当月1号是周几
 *
 * @param year
 * @param month
 * @return
 */
fun getFirstWeekOfMonth(year: Int, month: Int): Int {
    val calendar = Calendar.getInstance()
    calendar[year, month] = 1
    return calendar[Calendar.DAY_OF_WEEK] - 1
}

/**
 * 计算当前月需要显示几行
 *
 * @param year
 * @param month
 * @return
 */
fun getMonthRows(year: Int, month: Int): Int {
    val items = getFirstWeekOfMonth(year, month - 1) + getMonthDays(year, month)
    var rows = if (items % 7 == 0) items / 7 else items / 7 + 1
    if (rows == 4) {
        rows = 5
    }
    return rows
}